package com.anmingle.pswb.exception;


import cn.dev33.satoken.exception.NotLoginException;
import com.anmingle.common.R;
import com.anmingle.common.ResponseResult;
import com.anmingle.enums.ErrorCode;
import com.anmingle.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author https://github.com/liyupi
 *
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public ResponseResult<?> businessExceptionHandler(BusinessException e) {
        log.error("businessException: " + e.getMessage(), e);
        return R.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseResult<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return R.error(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public ResponseResult<?> NotLoginExceptionHandler(NotLoginException e) {
        log.error(e.getMessage());
        if ("未能读取到有效Token".equals(e.getMessage())) {
            return R.error(ErrorCode.NOT_LOGIN_ERROR);
        }
        return R.error(40100,"请重新登录");
    }
}
